/**
 * 
 */
package com.dhcc.DRGsHosp.facade.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import javax.inject.Named;

import org.dayatang.domain.InstanceFactory;
import org.dayatang.querychannel.QueryChannelService;
import org.openkoala.businesslog.MethodAlias;
import org.openkoala.koala.commons.InvokeResult;
import org.springframework.beans.factory.annotation.Autowired;

import com.dhcc.DRGsHosp.application.DictionaryApplication;
import com.dhcc.DRGsHosp.facade.DictionaryFacade;
import com.dhcc.DRGsHosp.facade.dto.DictionaryDTO;
import com.dhcc.DRGsHosp.facade.impl.assembler.DictionaryAssembler;
import com.dhcc.DRGsHosp.infra.util.MonitorResultUtil;

/**
 * @author chl
 *
 */
@Named("drgsHospFacadeImpl10")
public class DictionaryFacadeImpl implements DictionaryFacade {
	
	@Autowired
	private DictionaryApplication  application;
	
	
	private QueryChannelService queryChannel;

    private QueryChannelService getQueryChannelService(){
       if(queryChannel==null){
          queryChannel = InstanceFactory.getInstance(QueryChannelService.class,"queryChannel");
       }
     return queryChannel;
    }


	/**
	 * 
	 *  @Description：删除字典
	 *  @Author lhj 
	 *  @see com.dhcc.DRGsHosp.facade.DictionaryFacade#removeDictionary(java.lang.String)
	 */
	@Override
	public void removeDictionary(String id) {
		DictionaryDTO dictionaryDTO = new DictionaryDTO() ;
		dictionaryDTO.setDicCode(id);
		application.removeDictionary(DictionaryAssembler.toEntity(dictionaryDTO));
		application.removeDictionaryData(id);
	}

	/* (non-Javadoc)
	 * @see com.dhcc.DRGsHosp.facade.DictionaryFacade#getDictionary(java.lang.String)
	 */
	@Override
	public InvokeResult getDictionary(String dicCode) {
		List<Map<String, Object>> resultList = new ArrayList<Map<String,Object>>() ;
	    DictionaryDTO dicDto = DictionaryAssembler.toDTO(application.getDictionaryBydicCode(dicCode)) ;
	    Map< String, Object> map = new HashMap<String, Object>() ;
	    map.put("dicCode", dicDto.getDicCode()) ;
		map.put("dicDesc", dicDto.getDicDesc()) ;
		resultList.add(map) ;
		return InvokeResult.success(resultList); 
	}

	/**
	 * 
	 *  @Description：获取字典
	 *  @Author lhj 
	 *  @see com.dhcc.DRGsHosp.facade.DictionaryFacade#findAllDictionary()
	 */
	@Override
	@MethodAlias("query")
	public InvokeResult findAllDictionary() {
		List<Map<String, Object>> resultList = new LinkedList<>();
		List<DictionaryDTO> list = DictionaryAssembler.toDTOs(application.findAllDictionary());
		for(DictionaryDTO dic: list){
			Map<String, Object> map = new HashMap<>();;
			map.put("dicCode", dic.getDicCode());
			map.put("dicDesc", dic.getDicDesc());
			resultList.add(map);
		}
		return InvokeResult.success(resultList) ;
	}


	/**
	 * 
	 *  @Description：保存字典
	 *  @Author lhj 
	 *  @see com.dhcc.DRGsHosp.facade.DictionaryFacade#saveDictionary(java.util.Map)
	 */
	@Override
	public void saveDictionary(Map<String, Object> json) {
		 application.saveDictionary(json);
	}
	
	@Override
	public String findDictionaryValueByDesc(String dicCode, String dicdDesc){
		Map<String, Object> map = new HashMap<>();
		map.put("dicCode", dicCode);
		map.put("dicdDesc", dicdDesc);
		List<Object[]> list = application.findDictoryByParam(map);
		if(list.size()>0){
			Object[] objs = list.get(0);
			String value = MonitorResultUtil.object2String(objs[2]);
			return value;
		}else{
			return null;
		}
	}

/**
 * 
 *  @Description：获取字典
 *  @Author lhj 
 *  @see com.dhcc.DRGsHosp.facade.DictionaryFacade#findDictionaryByCode(java.lang.String)
 */
	@Override
	public InvokeResult findDictionaryByCode(String dicCode) {		
		return InvokeResult.success(application.findDictionaryByCode(dicCode));
	}

/**
 *  @Description：获取数据明细
 *  @Author lhj 
 *  @see com.dhcc.DRGsHosp.facade.DictionaryFacade#getDictionaryDataByCode(java.lang.String)
 */
@Override
public InvokeResult getDictionaryDataByCode(String dicCode) {
	return InvokeResult.success(application.getDictionaryDataByCode(dicCode));
 }

}
